package com.speridian.oms.custom;


import com.speridian.oms.view.beans.backing.UserSession;

import com.speridian.oms.view.beans.utils.JSFUtils;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;

import javax.faces.event.PhaseListener;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ws.types.BOUserLogin;

public class CustomPagePhaseListener  implements PhaseListener {
    private static final long serialVersionUID = 1L;

    public CustomPagePhaseListener() {
        super();
    }
    
    public void afterPhase(PhaseEvent pagePhaseEvent)
    {
      //    //System.out.println("afterPhase==>"+pagePhaseEvent.getPhaseId());

    }

    public void beforePhase(PhaseEvent pagePhaseEvent)
    {
      if (pagePhaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE)
      {
        ExternalContext extc =
          FacesContext.getCurrentInstance().getExternalContext();
        HttpServletResponse resp = (HttpServletResponse) extc.getResponse();
        HttpServletRequest request = (HttpServletRequest) extc.getRequest();
//        if(request.getSession().getAttribute("userLoginObj")!=null)
//        {
//          BOUserLogin userLoginObj = (BOUserLogin) request.getSession().getAttribute("userLoginObj");
//          Authentication authentication = new Authentication();
//          authentication.createUserSessionObject(userLoginObj);
//        }
        UserSession userSession =
          (UserSession) JSFUtils.resolveExpression("#{UserSession}");
        String viewId =
          pagePhaseEvent.getFacesContext().getViewRoot().getViewId();
        if (userSession == null || userSession.getUsercode() == null)
        {        
          if (!viewId.equals("/Login.jspx"))
          {
              String queryString =
                request.getQueryString().toString();  
              if(queryString.contains("reqId")){
                  String reqId=request.getParameter("reqId");
                  String url="../faces"+viewId+"?reqId="+reqId;
                  if(queryString.contains("tab")){
                      String tab=request.getParameter("tab");
                      url="../faces"+viewId+"?reqId="+reqId+"&tab="+tab;
                  }
                 JSFUtils.storeOnSession("savedUrl", url);
              }
            try
            {
              resp.sendRedirect("../");
              FacesContext.getCurrentInstance().responseComplete();
            }
            catch (IOException e)
            {
              throw new RuntimeException("CustomException during session invalidate. index.html not found");
            }
          }
        }
        else if (viewId.equals("/Login.jspx"))
        {
          try
          {
            resp.sendRedirect("../faces/Home.jspx");
           FacesContext.getCurrentInstance().responseComplete();
          }
          catch (IOException e)
          {
            throw new RuntimeException("CustomException during session invalidate. Home.jspx not found");
          }
        }
      }
    }

    public PhaseId getPhaseId()
    {
      return PhaseId.ANY_PHASE;
    }
}
